﻿CREATE OR REPLACE FUNCTION fn_check_duplicate_paymentno(p_payment_id integer,p_payment_no varchar(25))
  RETURNS boolean AS
$BODY$
declare
	chk_c boolean;
begin
	if coalesce(trim($2),'')='' then
		chk_c = true;
	else
		begin
			if exists(Select id from kdvn_request_of_payment krop where id<>$1 and paymentno ilike trim($2) and coalesce(trim(paymentno),'')<>'') then
				chk_c = false;
			else
				chk_c = true;
			end if;
		end;
	end if;
	return chk_c;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

  -- select paymentno from kdvn_request_of_payment where coalesce(paymentno,'')<>'' group by paymentno having count(paymentno)>1;
--   Select id from kdvn_request_of_Payment where paymentno='IN11-0401';
--   DELETE FROM KDVN_REQUEST_OF_PAYMENT WHERE ID=702;
  